package tools

import (
	"crypto/rand"
	"encoding/binary"
	"math/big"
)

// R is the mod of the finite field
var R, _ = new(big.Int).SetString(
	"21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)

func RandBigInt() (*big.Int, error) {
	maxbits := R.BitLen()
	b := make([]byte, (maxbits/8)-1)
	_, err := rand.Read(b)
	if err != nil {
		return nil, err
	}
	r := new(big.Int).SetBytes(b)
	rq := new(big.Int).Mod(r, R)

	return rq, nil
}

func Max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func Uint64ToByte(in uint64) []byte {
	b := make([]byte, 8)
	binary.LittleEndian.PutUint64(b, in)
	return b
}
